'use strict';
// +----------------------------------------------------------------------
// | CmPage [ 通用页面框架 ]
// +----------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0
// +----------------------------------------------------------------------
// | Author: defans <defans@sina.cn>
// +----------------------------------------------------------------------

/**
 单据流转的业务模块，实现了采购、库存管理相关的业务逻辑
 @module docu.model
 */

/**
 * 通用的单据行为实现类，单据保存于表 t_docu,t_order,t_order_apply,t_pay,t_xxxx_rec </br>
 * 相关表：t_goods, t_supplier, t_period,t_period_stock, 参数存于 t_code
 * @class docu.model.docu
 */

import CMPage from '../../cmpage/model/page_ms.js';

export default class extends CMPage {

    constructor(name, config = {}) {
        super(name,config);
        this.pk ='c_id';
    }
     /**
      * 取查询项的设置，结合POST参数，得到Where字句
      */
     async getQueryWhere(){
         //通过父类的方法取查询列设置解析后的where子句
         //this.pk = 'c_id';
         let where =await super.getQueryWhere();
        //  //此处增加额外的条件
        //  switch (this.type.id) {
        //      case cmpage.enumDocuType.INVALID:
        //          where += ` and c_status = -1 and c_group in(${this.mod.user.groups})`;
        //          break;
        //      default:
        //         where += ` and c_status<>-1 and c_type=${this.type.id} and c_group in(${this.mod.user.groups})`;
        //  }

         return where;
     }

     /**
      * 增加物料明细，根据模块名称判断数据来源，实现相应的逻辑
      * @method  goodsAdd
      * @return {object} 返回前端的状态对象
      */
      async goodsAdd(fromID, docuID){
          if(think.isEmpty(docuID))  return  {statusCode:300, message:`单据ID错误!`};          
          if(this.mod.c_modulename == 'OrderApplyRec'){
              let goods = await this.model('t_goods').where('c_id='+fromID).find();
              if(think.isEmpty(goods))  return  {statusCode:300, message:`物料ID不存在!`};
              let rec = {c_order_apply:docuID,c_goods:fromID,c_qty:1,c_supplier:0,c_time_arrive:think.datetime(),c_close:0,c_qty_to:0};
              let recID = await this.model('t_order_apply_rec').add(rec);
              return  {statusCode:recID>0 ? 200:300, message: recID>0 ? "":"操作失败！"};
          }

          return  {statusCode:300, message:`模块名称 ${this.mod.c_modulename} 错误！`};
      }

     /**
      * 取结果数据集，子类中重写本方法可以增加逻辑如：对结果集做进一步的数据处理等
      * @method  getDataList
      * @return {object} 结果集数据包 {count:xxx, list:[{record}]}
      */
    //  async getDataList(){
    //      let list = await this.getDataList();
    //      //去掉每行显示重复的项目，如单号、仓库等主表的信息
    //      let docuID =0;
    //      for(let rec of list){
    //          if(rec.c_docu == docuID){
    //              rec.c_no ='';
    //              rec.c_stock='';
    //              rec.c_stock_to = '';   //多加一个属性是不会死的 ^_^
    //              rec.c_user='';
    //              rec.c_time='';
    //          }else {
    //              docuID = rec.c_docu;
    //          }
    //      }
    // }



}
